package com.xgd.phylw.service.impl;

import com.xgd.phylw.bean.Admin;
import com.xgd.phylw.bean.AdminExample;
import com.xgd.phylw.bean.Member;
import com.xgd.phylw.bean.MemberExample;
import com.xgd.phylw.mapper.AdminMapper;
import com.xgd.phylw.mapper.MemberMapper;
import com.xgd.phylw.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private AdminMapper adminMapper;

    public int selectMember(Member member) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMemberNameEqualTo(member.getMemberName());
        List<Member> members = memberMapper.selectByExample(example);
        if(members!=null) {
            for (Member m : members) {
                if (m.getMemberPass().equals(member.getMemberPass())) {
                    return 1;
                }
                return -1;
            }
        }
        return 0;
    }

    public int selectAdmin(Admin admin) {
        AdminExample example = new AdminExample();
        example.createCriteria().andAdminNameEqualTo(admin.getAdminName());
        List<Admin> admins = adminMapper.selectByExample(example);
        if(admins!=null) {
            for (Admin a : admins) {
                if (a.getAdminPass().equals(admin.getAdminPass())) {
                    return 1;
                }
                return -1;
            }
        }
        return 0;
    }

    public int insertMember(Member member) {
        return memberMapper.insertSelective(member);
    }

    public Member queryMember(Member member) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMemberNameEqualTo(member.getMemberName());
        List<Member> members = memberMapper.selectByExample(example);
        for (Member m : members) {
            return m;
        }
        return null;
    }

    public int changeMember(Member member,String name) {
        MemberExample example = new MemberExample();
        example.createCriteria().andMemberNameEqualTo(name);
        return memberMapper.updateByExampleSelective(member,example);
    }

    public int usersDelete(String[] name) {
        return memberMapper.deleteByName(name);
    }

    public List<Member> MemberAll(String userName) {
        return memberMapper.LikeMember("%"+userName+"%");
    }

    public Admin queryAdmin(Admin admin) {
        AdminExample example = new AdminExample();
        example.createCriteria().andAdminNameEqualTo(admin.getAdminName());
        List<Admin> admins = adminMapper.selectByExample(example);
        if(admins!=null) {
            for (Admin a : admins) {
                return a;
            }
        }
        return null;
    }
}
